/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vadung.contragame2d;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.lcdui.Graphics;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author dung.voanh
 */
public class StateNewGame extends State { 

    private long m_timeShowPlayerDie; 
    
    public StateNewGame() {
    }

    public void Init() {
        m_timeShowPlayerDie = 0;
        MyObject.GetInstance().ClearAll();
        try {
            GameLayerManager.GetInstance().Load("/map01.txt");
    //        MyObject.GetInstance().Add(DEFINE.OBJECT_TYPE_ENEMY1, 500, 110);
    //        MyObject.GetInstance().Add(DEFINE.OBJECT_TYPE_ENEMY2, 700, 110);
            SoundManger.GetInstance().PlaySound(SoundManger.s_iSound_Base);
            
            RMSManager rs = RMSManager.GetInstance();
            rs.OpenRecordStore(DEFINE.STR_RMS_MANAGER);
            if(rs.GetRecordStore() == null || rs.GetRecordStore().getNumRecords() <= 0){ 
                MyObject.GetInstance().Add(DEFINE.OBJECT_TYPE_PLAYER, 0, 0);     
            }else{
                LoadGame();
            }
            
//            if(MyObject.s_player.ISDie()){ 
//                MyObject.GetInstance().Add(DEFINE.OBJECT_TYPE_PLAYER, MyObject.s_player.m_iXMap, 0);
//            }
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
    }

    public void Update() {
        MyObject.GetInstance().Update();
        
        if(MyObject.s_player.ISDie()){  
            if(m_timeShowPlayerDie == 0){
                m_timeShowPlayerDie = System.currentTimeMillis();   
            }
            if(Midlet.s_canGame.GetGameTimeLastUpdate() - m_timeShowPlayerDie >= 2000){       
                Midlet.s_canGame.SwitchState(DEFINE.STATE_MAINMENU);      
            }
        }
    }

    public void Render(Graphics g) {;
        int height = Midlet.s_canGame.getHeight();
        int width = Midlet.s_canGame.getWidth();
        g.setColor(0, 0, 0);
        g.fillRect(0, 0, width, height);

        GameLayerManager.GetInstance().Rendder(g);
        MyObject.GetInstance().Render(g);
    }

    public void Exit() {
    }

    //------------------------------------------------
    public void SaveGame() {
        RMSManager rs = RMSManager.GetInstance();
        try {
            if(MyObject.s_player.ISDie()){
                return;
            }
            rs.OpenRecordStore(DEFINE.STR_RMS_MANAGER);
            //Save position ò map of Contra 
            ByteArrayOutputStream bstream1 = new ByteArrayOutputStream(12);
            DataOutputStream ostream1 = new DataOutputStream(bstream1); 
            
            ostream1.writeInt(DEFINE.OBJECT_TYPE_MAP);
            ostream1.writeInt(GameLayerManager.GetInstance().GetPositionX());
            ostream1.writeInt(GameLayerManager.GetInstance().GetPositionY());
            ostream1.flush();
            ostream1.close();

            byte[] record1 = bstream1.toByteArray();
            rs.WriteRecordStore("", record1);
               
            //Save state of Contra 
             bstream1 = new ByteArrayOutputStream(12);
             ostream1 = new DataOutputStream(bstream1); 
            
            ostream1.writeInt(DEFINE.OBJECT_TYPE_PLAYER);
            ostream1.writeInt(MyObject.s_player.m_iXMap);
            ostream1.writeInt(MyObject.s_player.m_iYMap);
            ostream1.flush();
            ostream1.close();

            record1 = bstream1.toByteArray();
            rs.WriteRecordStore("", record1);
               
            //------------------------------------------------------------------
            //Save enemy 
            for (int i = 0; i < MyObject.s_listEnemy.size(); i++) {
                MyActor actor = (MyActor) MyObject.s_listEnemy.elementAt(i);
                if(actor.ISDie()){
                    continue;
                }
                ByteArrayOutputStream bstream = new ByteArrayOutputStream(12);
                DataOutputStream ostream = new DataOutputStream(bstream);
                if (actor instanceof MyEnemy) {
                    ostream.writeInt(DEFINE.OBJECT_TYPE_ENEMY1);
                } else {
                    ostream.writeInt(DEFINE.OBJECT_TYPE_ENEMY2);
                }
                ostream.writeInt(actor.m_iXMap);
                ostream.writeInt(actor.m_iYMap);
                ostream.flush();
                ostream.close();

                byte[] record = bstream.toByteArray();
                rs.WriteRecordStore("", record);
            }
            rs.CloseRecordStore();
            System.out.println("---SAVE GAME---");
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }

    public void LoadGame() {
        System.out.println("---LOAD GAME---");
        RMSManager rs = RMSManager.GetInstance();
        try {
            rs.OpenRecordStore(DEFINE.STR_RMS_MANAGER);
            if(rs.GetRecordStore().getNumRecords() <= 0){
                return;
            }
            MyObject.GetInstance().ClearAll();
            int typeObject;
            int x, y; 
            
            int rsSize = rs.GetRecordStore().getNumRecords();
            for(int i = 1; i < rsSize; i++){
                byte[] record = rs.ReadRecordStore(i);
                DataInputStream istream = new DataInputStream(new ByteArrayInputStream(record, 0, record.length));
                typeObject = istream.readInt();
                x = istream.readInt();
                y = istream.readInt();
                
                if(typeObject == DEFINE.OBJECT_TYPE_MAP){
                    GameLayerManager.GetInstance().SetViewWindows(x, y);
                }else{
                    MyObject.GetInstance().Add(typeObject, x, y);
                } 
            }
            
            rs.CloseRecordStore();
            rs.deleteRecStore(DEFINE.STR_RMS_MANAGER);
            System.out.println("---END LOAD GAME---");
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
}
